import { post, get } from '@/utils/request'
import axios from 'axios'

/**
 * 素材库相关接口
 */
export const sourceApi = {
  // 获取素材列表
  getList: (params) => post('/sourceApi/getList', params),

  // 获取素材分组列表
  getGroups: () => get('/sourceApi/getGroups'),

  // 上传素材文件
  uploadSource: (files, groupId) => {
    const formData = new FormData()

    // 添加文件到表单数据
    if (Array.isArray(files)) {
      files.forEach((file) => {
        formData.append('files', file)
      })
    } else {
      formData.append('files', files)
    }

    // 添加分组ID
    formData.append('groupId', groupId === null ? '-1' : String(groupId))

    return post('/sourceApi/uploadSource', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    })
  },

  // 删除素材（单个或批量）
  deleteSource: (delIds) => {
    // 确保ids是数组
    const idList = Array.isArray(delIds) ? delIds : [delIds]

    return post('/sourceApi/batchDelete', { ids: idList })
  },

  // 移动素材到分组（单个或批量）
  moveToGroup: (ids, groupId) => {
    // 确保ids是数组
    const idList = Array.isArray(ids) ? ids : [ids]

    return post('/sourceApi/batchMoveToGroup', {
      ids: idList,
      groupId: groupId === null ? '-1' : String(groupId),
    })
  },

  // 创建分组
  createGroup: (name, description) => post('/sourceApi/createGroup', { name, description }),

  // 删除分组
  deleteGroup: (id) => post('/sourceApi/deleteGroup', { id }),

  // 更新分组
  updateGroup: (id, name, description) => post('/sourceApi/updateGroup', { id, name, description }),

  // 获取各文件类型数量统计
  getSourceCount: () => get('/sourceApi/getSourceCount'),
}
